<?php


namespace app\admin\model;


use think\facade\Db;
use think\Model;

class Common extends Model
{
    protected $id = null;
    protected $table = null;
    protected $scheme = null;

    public function __construct($id, $table)
    {
        $this->id = $id;
        $this->table = $table;
        $this->scheme = new Scheme();
    }

    /**
     * @param $id //当前ID
     * @param $table //表名称
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取当前流程图和审批记录
     */
    public function getCheckFlow()
    {
        $res = Db::name($this->table)->find($this->id);
        $row = $this->scheme->where('id', $res['flow_id'])->find();
        $this->task = new FlowTask();
        $task = $this->task->where(['from_table' => $this->table, 'from_id' => $this->id, 'status' => 0])->find();
        if(empty($task)){
            $task = $this->task->where(['from_table' => $this->table, 'from_id' => $this->id, 'status' => 2])->find();
        }
        $activityId = $task['stepid'];
        $taskList = Db::name('flow_task')
            ->alias('main')
            ->join('admins admin', 'admin.id=main.receiveid', 'LEFT')
            ->where('instanceid', $task['instanceid'])
            ->field(["main.instanceid", "main.stepid", "main.status", "admin.nickname", "main.completedtime", "main.createtime"])
            ->order('main.createtime', 'desc')
            ->select();
        $history = Db::name('flow_task')
            ->alias('main')
            ->join('admins admin', 'admin.id=main.receiveid', 'LEFT')
            ->where(['instanceid' => $task['instanceid'], 'main.status' => 2])
            ->field(["main.receiveid", "main.stepname", "main.comment", "admin.nickname", "main.completedtime"])
            ->order('main.createtime asc,main.completedtime asc')
            ->select();
        $data = [
            'taskList' => $taskList,
            'activityId' => $activityId,
            'history' => $history,
            'flowcontent' => $row['flowcontent']
        ];
        return $data;
    }
}